package com.caiwangdai.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.caiwangdai.app.R;


//摄像机遮罩
public class CameraMask extends View {


    private Paint transparentP;
    private Paint borderP;

    private RectF fullRect;
    private RectF rectCenter;


    private PorterDuffXfermode mixMode;

    private int dstColor = -1;
    private int srcColor = -1;
    private int borderLength = dip2px(30);
    private int borderWidth = dip2px(4);
    private int borderGap = borderWidth / 2;

    public CameraMask(Context context) {
        this(context, null);
    }

    public CameraMask(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraMask(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        dstColor = ContextCompat.getColor(getContext(), R.color.black_alpha60);
        srcColor = ContextCompat.getColor(getContext(), R.color.transparent);

        transparentP = new Paint();
        transparentP.setColor(dstColor);
        transparentP.setStyle(Paint.Style.FILL);
        transparentP.setAntiAlias(true);


        borderP = new Paint();
        borderP.setStrokeWidth(borderWidth);
        borderP.setColor(Color.WHITE);
        borderP.setStyle(Paint.Style.FILL);
        borderP.setAntiAlias(true);

        mixMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int centerX = w / 2-dip2px(16);
        int centerY = h / 2;
        fullRect = new RectF(0, 0, w, h);


        int cardHeight = dip2px(283);
        int cardWidth = dip2px(450);


        int borderLeft = centerX - cardWidth / 2;
        int borderTop = centerY - cardHeight / 2;
        int borderRight = centerX + cardWidth / 2;
        int borderBottom = centerY + cardHeight / 2;
        rectCenter = new RectF(borderLeft, borderTop, borderRight, borderBottom);


    }

    @Override
    protected void onDraw(Canvas canvas) {

        //save as new layer
        int sc = canvas.saveLayer(fullRect, borderP, Canvas.ALL_SAVE_FLAG);

        //dst层
        transparentP.setColor(dstColor);
        canvas.drawRect(fullRect, transparentP);


        transparentP.setColor(srcColor);
        transparentP.setXfermode(mixMode);

        //src层
        canvas.drawRect(rectCenter, transparentP);

        //restore to canvas
        transparentP.setXfermode(null);
        canvas.restoreToCount(sc);

        //draw whit border

        float borderLeft = rectCenter.left;
        float borderRight = rectCenter.right;
        float borderTop = rectCenter.top;
        float borderBottom = rectCenter.bottom;

        //left-top
        canvas.drawLine(borderLeft, borderTop + borderGap, borderLeft + borderLength, borderTop + borderGap, borderP);
        canvas.drawLine(borderLeft + borderGap, borderTop, borderLeft + borderGap, borderTop + borderLength, borderP);

        //right-top
        canvas.drawLine(borderRight, borderTop + borderGap, borderRight - borderLength, borderTop + borderGap, borderP);
        canvas.drawLine(borderRight - borderGap, borderTop, borderRight - borderGap, borderTop + borderLength, borderP);

        //left-bottom
        canvas.drawLine(borderLeft, borderBottom - borderGap, borderLeft + borderLength, borderBottom - borderGap, borderP);
        canvas.drawLine(borderLeft + borderGap, borderBottom, borderLeft + borderGap, borderBottom - borderLength, borderP);

        //right-bottom
        canvas.drawLine(borderRight, borderBottom - borderGap, borderRight - borderLength, borderBottom - borderGap, borderP);
        canvas.drawLine(borderRight - borderGap, borderBottom, borderRight - borderGap, borderBottom - borderLength, borderP);


    }


    //根据手机的分辨率从 dp 的单位 转成为 px(像素)
    public int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
